package com.tsd.system.service.impl;

import com.tsd.core.constants.Constants;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SqlSelectMapper;
import com.tsd.system.dao.SysParameterMapper;
import com.tsd.system.entity.SysParameter;
import com.tsd.system.service.SysParameterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysParameter 服务实现层
 *
 * @author Hillpool
 * @date 2019/03/18 17:43:03
 */
@Service
public class SysParameterServiceImpl extends BaseServiceImpl implements SysParameterService {

    @Resource
    private SysParameterMapper sysParameterMapper;
    @Resource
    private SqlSelectMapper sqlSelectMapper;

    @Override
    public SysParameter queryByPrimaryKey(Long id) throws Exception {
        return sysParameterMapper.selectByPrimaryKey(id);
    }

    @Override
    public SysParameter queryBySid(String sid) throws Exception {
        return sysParameterMapper.selectBySid(sid);
    }

    @Override
    public SysParameter queryByName(String name) throws Exception {
        return sysParameterMapper.selectByName(name);
    }

    @Override
    public void deleteByPrimaryKey(Long id) throws Exception {
        sysParameterMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveSysParameter(SysParameter record, String opUserSid) throws Exception {
        super.checkEmpty(record.getName(), "缺少参数name");
        if (HlpUtils.isEmpty(record.getId())) {
            SysParameter old = sysParameterMapper.selectByName(record.getName());
            if (old != null) {
                record.setId(old.getId());
                record.setUpdateDate(new Date());
                record.setUpdatorId(opUserSid);
                sysParameterMapper.updateByPrimaryKeySelective(record);
            } else {
                record.setSid(super.getPrimaryKey());
                record.setCreateDate(new Date());
                record.setCreatorId(opUserSid);
                record.setState(Constants.USER_STATUS_VALID);
                record.setDr(Constants.DR_VALID);
                sysParameterMapper.insertSelective(record);
            }
        } else {
            record.setUpdateDate(new Date());
            record.setUpdatorId(opUserSid);
            sysParameterMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public List<SysParameter> findSysParameters(Map<String, Object> params, PageBean page) throws Exception {
        return sysParameterMapper.selectByPage(params, page);
    }

    @Override
    public List<SysParameter> findSysParameters(Map<String, Object> params) throws Exception {
        return sysParameterMapper.selectByParams(params);
    }
}
